<?php
/*
 * Copyright 2013 by Jerrick Hoang, Ivy Xing, Sam Roberts, James Cook, 
 * Johnny Coster, Judy Yang, Jackson Moniaga, Oliver Radwan, 
 * Maxwell Palmer, Nolan McNair, Taylor Talmage, and Allen Tucker. 
 * This program is part of RMH Homebase, which is free software.  It comes with 
 * absolutely no warranty. You can redistribute and/or modify it under the terms 
 * of the GNU General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 * 
 */

/**
 * @version March 18, 2014
 * @author Joseph Chang
 * change log: changed all functions for functionality for Forestdale
 */
include_once('dbinfo.php');
include_once(dirname(__FILE__) . '/../domain/Client.php');

/*function create_dbClient() {
    connect();
    mysql_query("DROP TABLE IF EXISTS dbClient");
    $result = mysql_query("CREATE TABLE dbClient(clientID TEXT NOT NULL, first_name TEXT NOT NULL, last_name TEXT, ssn TEXT, " .
            " gender TEXT, birthday TEXT, phone1 VARCHAR(12) NOT NULL, phone2 VARCHAR(12), status TEXT, medication_taken TEXT, " .
            " admission_physical_notes TEXT, discharge_physical_notes TEXT");
    
    if (!$result)
        echo mysql_error() . "Error creating dbClient table<br>";
} */

// add a client to dbClient table: if already there, return false 
function add_client($client) {
    error_log('in add_client, client id is '. $client->get_clientID());
   error_log('client ssn is '.$client->get_SSN()) ;
    if (!$client instanceof Client)
        die("Error: add_client type mismatch");
    
    connect();
    $query = "SELECT * FROM dbClient WHERE clientID = '" . $client->get_clientID() . "'";
    $result = mysql_query($query);
    
    if (!$result)
    {
        error_log('ERROR on select in add_client '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    
    //if there's no entry for this id, add it
    if ($result == null || mysql_num_rows($result) == 0) {
         $insertQuery='INSERT INTO dbClient (clientID,first_name,last_name,ssn,gender,birthday,'
                 . ' phone1,phone2,status,medication_taken,admission_physical_notes,discharge_physical_notes) '
                . 'VALUES("' .
                $client->get_ClientID() . '","' .
                $client->get_first_name() . '","' .
                $client->get_last_name() . '","' .
                $client->get_SSN() . '","' .
                $client->get_gender() . '","' .
                $client->get_birthday() . '","' .
                $client->get_phone1() . '","' .
                $client->get_phone2() . '","' .
                $client->get_status() . '","' .
                $client->get_medication_taken() . '","' .
                $client->get_admission_physical_notes() . '","' .
                $client->get_discharge_physical_notes()  . '");';
         error_log('insert query in add_client is '.$insertQuery);
         $insertRes = mysql_query($insertQuery);
        if (!$insertRes) {
            error_log('ERROR on insert in add_client ' . mysql_error());
            die('Invalid query: ' . mysql_error());
        }
        mysql_close();
        return true;
    }
    mysql_close();
    return false;
}

// remove a client from dbClient table.  If already there, return 
function remove_client($clientID) {
    connect();
    $query = 'SELECT * FROM dbClient WHERE clientID = "' . $clientID . '"';
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on select in remove_client '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if ($result == null || mysql_num_rows($result) == 0) {
        mysql_close();
        return false;
    }
    $query = 'DELETE FROM dbClient WHERE clientID = "' . $clientID . '"';
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on delete in add_client '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    mysql_close();
    return true;
}

/*
 * @return a client from dbClient table matching a particular id.
 * if not in table, return false
 */
function retrieve_client($clientID) {
    connect();
    $query = "SELECT * FROM dbClient WHERE clientID = '" . $clientID . "'";
    $result = mysql_query($query);
    error_log("The query is: ".$result);
    if (!$result)
    {
        error_log('ERROR on select in retrieve_client '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    // var_dump($result_row);
    $theClient = make_a_client($result_row);
    // mysql_close();
    return $theClient;
}

// Name is first concat with last name. (e.g.) 'Tom the Cat'
// return array of Client
function retrieve_client_by_name ($name) {
	$client = array();
	if (!isset($name) || $name == "" || $name == null) return $client;
	connect();
	$name = explode(" ", $name);
	$first_name = $name[0];
	$last_name = $name[1];
    $query = "SELECT * FROM dbClient WHERE first_name = '" . $first_name . "' AND last_name = '". $last_name ."'";
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on select in retrieve_client_by_name '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    while ($result_row = mysql_fetch_assoc($result)) {
        $the_client = make_a_client($result_row);
        $client[] = $the_client;
    }
    return $client;	
}

// Returns clients given a ssn.
function retrieve_client_by_ssn($ssn) {
    connect();
    $query = "SELECT * FROM dbClient WHERE ssn = '" . $ssn . "'";
    $result = mysql_query($query);
    error_log("The query is: ".$result);
    if (!$result)
    {
        error_log('ERROR on select in retrieve_client '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    // var_dump($result_row);
    $theClient = make_a_client($result_row);
    // mysql_close();
    return $theClient;
}

// Returns clients given a status.
function retrieve_client_by_status($status) {
    connect();
    $query = "SELECT * FROM dbClient WHERE status = '" . $status . "'";
    $result = mysql_query($query);
    error_log("The query is: ".$result);
    if (!$result)
    {
        error_log('ERROR on select in retrieve_client '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if (mysql_num_rows($result) !== 1) {
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    // var_dump($result_row);
    $theClient = make_a_client($result_row);
    // mysql_close();
    return $theClient;
}

/*
 * @return all rows from dbClient table ordered by last name
 * if none there, return false
 */
function getall_dbClient() {
    connect();
    $query = "SELECT * FROM dbClient ORDER BY last_name,first_name";
    $result = mysql_query($query);
    
    if (!$result)
    {
        error_log('ERROR on select in getall_dbClient '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    
    if ($result == null || mysql_num_rows($result) == 0) {
        mysql_close();
        return false;
    }
   
    $result = mysql_query($query);
    $theClient = array();
   
    while ($result_row = mysql_fetch_assoc($result)) {
        $client = make_a_client($result_row);
        $theClient[] = $client;
    }

    return $theClient;
}

function make_a_client($result_row) {
    $theClient = new Client(
                    $result_row['first_name'],
                    $result_row['last_name'],
                    $result_row['ssn'],
                    $result_row['gender'],
                    $result_row['birthday'],
                    $result_row['phone1'],
                    $result_row['phone2'],
                    $result_row['status'],
                    $result_row['medication_taken'],
                    $result_row['admission_physical_notes'],
                    $result_row['discharge_physical_notes']);
    return $theClient;
}

// format's phone number for database entry
function phone_edit($phone) {
    if ($phone!="")
		return substr($phone, 0, 3) . "-" . substr($phone, 3, 3) . "-" . substr($phone, 6);
	else return "";
}
?>
